package com.codbking.calendar.exaple.richeditor;

import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.SpannableString;
import android.text.TextWatcher;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.text.style.UnderlineSpan;
import android.util.AttributeSet;
import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;

import com.codbking.calendar.exaple.R;

public class RichEditor extends ScrollView {
    private LinearLayout linearLayout;
    private EditText currentEditText;
    private String TAG = "RichEditor";
    private SparseArray<AbstractTextStyle> spanStyleSparseArray;
    private OnClickListener onClickListener;
    private TextWatcher textWatcher;

    private ClipboardManager clipboardManager;

    public RichEditor(Context context) {
        super(context);
        init(context);
    }

    public RichEditor(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public RichEditor(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(final Context context) {
        linearLayout = new LinearLayout(context);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        linearLayout.setBackgroundColor(Color.WHITE);
        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT);
        addView(linearLayout, layoutParams);
        TextStyleFactory.textColor = 0xff000000;
        TextStyleFactory.textBgColor = 0xffffffff;
        spanStyleSparseArray = new SparseArray<>();
        spanStyleSparseArray.put(TextStyleFactory.UNDERLINE_SPAN, new TextStyle<>(UnderlineSpan.class, TextStyleFactory.UNDERLINE_SPAN));
        spanStyleSparseArray.put(TextStyleFactory.FOREGROUND_COLOR_SPAN, new TextColorStyle<>(ForegroundColorSpan.class, TextStyleFactory.FOREGROUND_COLOR_SPAN));
        spanStyleSparseArray.put(TextStyleFactory.BACKGROUND_COLOR_SPAN, new TextBgColorStyle<>(BackgroundColorSpan.class, TextStyleFactory.BACKGROUND_COLOR_SPAN));
        spanStyleSparseArray.put(TextStyleFactory.BOLD_SPAN, new BoldStyle<>(StyleSpan.class, TextStyleFactory.BOLD_SPAN));
        spanStyleSparseArray.put(TextStyleFactory.ITALIC_SPAN, new ItalicStyle<>(StyleSpan.class, TextStyleFactory.ITALIC_SPAN));
        onClickListener = new OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "onclick " + currentEditText.getSelectionStart() + " " + currentEditText.getSelectionEnd());
                cursorPositionChangeListener.setCursorPositionChangeListener(TextStyleFactory.NORMAL_SPAN);
                for (int i = 0; i < spanStyleSparseArray.size(); i++) {
                    spanStyleSparseArray.valueAt(i).isSelect = false;
                }
                if (currentEditText.getSelectionStart() > 0) {
                    for (int i = 0; i < spanStyleSparseArray.size(); i++) {
                        spanStyleSparseArray.valueAt(i).cursorPositionChanged(currentEditText, currentEditText.getSelectionStart(), 0);
                    }
                }
            }
        };

        textWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                Log.d(TAG, "beforeTextChanged " + s.toString() + " start " + start + " count " + count + " after " + after);
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Log.d(TAG, "onTextChanged " + s.toString() + " start " + start + " before " + before + " count " + count);
                if (count > 0) {
                    for (int i = 0; i < spanStyleSparseArray.size(); i++) {
                        spanStyleSparseArray.valueAt(i).inputTextChanged(currentEditText, start, count);
                    }
                }

                if (before > 0) {
                    for (int i = 0; i < spanStyleSparseArray.size(); i++) {
                        spanStyleSparseArray.valueAt(i).backspaceTextChanged(currentEditText, start, before);
                    }
                    cursorPositionChangeListener.setCursorPositionChangeListener(TextStyleFactory.NORMAL_SPAN);
                    for (int i = 0; i < spanStyleSparseArray.size(); i++) {
                        spanStyleSparseArray.valueAt(i).isSelect = false;
                    }
                    if (currentEditText.getSelectionStart() > 0) {
                        for (int i = 0; i < spanStyleSparseArray.size(); i++) {
                            spanStyleSparseArray.valueAt(i).cursorPositionChanged(currentEditText, start, -before);
                        }
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                Log.d(TAG, "afterTextChanged " + s.toString());
            }
        };

        currentEditText = (EditText) LayoutInflater.from(context).inflate(R.layout.edittext, null);
        linearLayout.addView(currentEditText);
        currentEditText.setOnClickListener(onClickListener);
        currentEditText.addTextChangedListener(textWatcher);

        //TODO
//        clipboardManager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
//        clipboardManager.addPrimaryClipChangedListener(new ClipboardManager.OnPrimaryClipChangedListener() {
//            @Override
//            public void onPrimaryClipChanged() {
//                CharSequence str = clipboardManager.getPrimaryClip().getItemAt(0).getText();
//                SpannableString spannableStringBuilder = (SpannableString) str;
//                StyleSpan[] typeSpans = currentEditText.getEditableText().getSpans(0, spannableStringBuilder.length(), StyleSpan.class);
//                for (StyleSpan s : typeSpans) {
//                    Log.d("aaaaa ", "aaaa " + spannableStringBuilder.getSpanStart(s) + "  " + spannableStringBuilder.getSpanEnd(s));
//                }
//            }
//        });
    }

    public void setBullet() {

    }

    public void setBold() {
        if (spanStyleSparseArray.get(TextStyleFactory.BOLD_SPAN) != null) {
            spanStyleSparseArray.get(TextStyleFactory.BOLD_SPAN).changeSelect();
        }
    }

    public void setItalic() {
        if (spanStyleSparseArray.get(TextStyleFactory.ITALIC_SPAN) != null) {
            spanStyleSparseArray.get(TextStyleFactory.ITALIC_SPAN).changeSelect();
        }
    }

    public void setUnderLine() {
        if (spanStyleSparseArray.get(TextStyleFactory.UNDERLINE_SPAN) != null) {
            spanStyleSparseArray.get(TextStyleFactory.UNDERLINE_SPAN).changeSelect();
        }
    }

    public EditText getCurrentEditText() {
        return currentEditText;
    }

    private CursorPositionChangeListener cursorPositionChangeListener = new CursorPositionChangeListener() {
        @Override
        public void setCursorPositionChangeListener(int textStyle) {
        }
    };

    public void setCursorPositionChangeListener(CursorPositionChangeListener cursorPositionChangeListener) {
        this.cursorPositionChangeListener = cursorPositionChangeListener;
        for (int i = 0; i < spanStyleSparseArray.size(); i++) {
            spanStyleSparseArray.valueAt(i).setCursorPositionChangeListener(cursorPositionChangeListener);
        }
    }
}
